package com.fengye.middleware.rabbitmq.boot.ack.auto;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengye.middleware.rabbitmq.boot.ack.KnowledgeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 *
 * @author sky
 * @date 2021年06月09日 11:22 上午
 */
@Component
@Slf4j
public class KnowledgeAutoConsumer {

    @Autowired
    private ObjectMapper objectMapper;


    @RabbitListener(queues = {"${mq.knowledge.auto.queue.name:auto_queue_name}"}, containerFactory = "singleListenerContainerAuto")
    public void consumerMsg(@Payload byte[] msg) {
        try {
            KnowledgeInfo info = objectMapper.readValue(msg, KnowledgeInfo.class);

            log.info("基于 AUTO 机制：消费者-接收消息：{}", info);
        } catch (Exception e) {
            log.error("基于 AUTO 机制：消费者-接收消息异常：", e.fillInStackTrace());
        }
    }
}
